package com.cloudxingkong.dianyinke.Student.Ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Fragment.DFH_Fragment;
import com.cloudxingkong.dianyinke.Student.Fragment.DFK_Fragment;
import com.cloudxingkong.dianyinke.Student.Fragment.DPJ_Fragment;
import com.cloudxingkong.dianyinke.Student.Fragment.DSH_Fragment;
import com.cloudxingkong.dianyinke.Student.Fragment.QBFK_Fragment;
import com.cloudxingkong.dianyinke.Student.bean.MyChangerEntity;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * @我的兑换Activity
 */
public class MyChange extends BaseActivity implements DFK_Fragment.getIsPayed {
    private RadioGroup radioGroup;
    private RadioButton radioBtn_quanbufukuan, radioBtn_daifukuan, radioBtn_daifahuo, radioBtn_daishouhuo, radioBtn_daipingjia;
    private FragmentManager fm;
    private FrameLayout frameLayout;
    private List<MyChangerEntity.ObjBean> list;
    private SharedPreferences sharedPreferences;


    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.activity_my_change);
        initView();//初始化控件
        sharedPreferences = MyChange.this.getSharedPreferences("data_dyk", 0);
        fm = getSupportFragmentManager();
        FragmentTransaction trans = fm.beginTransaction();
        trans.replace(R.id.mc_framelayout, new QBFK_Fragment());
        trans.commit();
        loadData();
    }

    private void loadData() {
        final String sessionid = CookieHelper.getSessionID(sharedPreferences);
        final String upLoadUrl = ThisUri.SERVER_YUMING + StudentUri.MYCHANGE;
        RequestParams params = new RequestParams(upLoadUrl);
        params.addHeader("cookie", sessionid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "全部付款" + result);
                Message message = handler.obtainMessage();
                message.what = 1;
                message.obj = result;
                handler.sendMessage(message);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void initView() {
        list = new ArrayList<>();
        radioGroup = (RadioGroup) findViewById(R.id.mc_radiogroup);
        radioBtn_quanbufukuan = (RadioButton) findViewById(R.id.radioBtn_quanbufukuan);
        radioBtn_daifukuan = (RadioButton) findViewById(R.id.radioBtn_daifukuan);
        radioBtn_daifahuo = (RadioButton) findViewById(R.id.radioBtn_daifahuo);
        radioBtn_daishouhuo = (RadioButton) findViewById(R.id.radioBtn_daishouhuo);
        radioBtn_daipingjia = (RadioButton) findViewById(R.id.radioBtn_daipingjia);
        frameLayout = (FrameLayout) findViewById(R.id.mc_framelayout);
    }

    public void pay(View view) {
        switch (view.getId()) {
            case R.id.radioBtn_quanbufukuan:  //全部付款
                FragmentTransaction trans1 = fm.beginTransaction();
                trans1.replace(R.id.mc_framelayout, new QBFK_Fragment());
                trans1.commit();
                break;
            case R.id.radioBtn_daifukuan:  //待付款
                FragmentTransaction trans2 = fm.beginTransaction();
                trans2.replace(R.id.mc_framelayout, new DFK_Fragment());
                trans2.commit();
                break;
            case R.id.radioBtn_daifahuo:   //待发货
                FragmentTransaction trans3 = fm.beginTransaction();
                trans3.replace(R.id.mc_framelayout, new DFH_Fragment());
                trans3.commit();
                break;
            case R.id.radioBtn_daishouhuo:  //待收货
                FragmentTransaction trans4 = fm.beginTransaction();
                trans4.replace(R.id.mc_framelayout, new DSH_Fragment());
                trans4.commit();
                break;
            case R.id.radioBtn_daipingjia: //待评价
                FragmentTransaction trans5 = fm.beginTransaction();
                trans5.replace(R.id.mc_framelayout, new DPJ_Fragment());
                trans5.commit();
                break;
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                String state = (String) msg.obj;
                if (state != null) {
                    Gson gson = new Gson();
                    MyChangerEntity entity = gson.fromJson(state, MyChangerEntity.class);
                    if (entity.getObj() != null) {
                        radioBtn_quanbufukuan.setText("全部付款\n" + entity.getObj().getAll());
                        radioBtn_daifukuan.setText("待付款\n" + entity.getObj().getWpay());
                        radioBtn_daifahuo.setText("待发货\n" + entity.getObj().getWdelivery());
                        radioBtn_daishouhuo.setText("待收货\n" + entity.getObj().getWevaluation());
                        radioBtn_daipingjia.setText("待评价\n" + entity.getObj().getWreceiving());
                        radioBtn_quanbufukuan.setChecked(true);
                    } else {
                        Toast.makeText(MyChange.this, "数据为空", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    };

    @Override
    public void getPayed(int index) {
        Log.e("test", "是否支付 " + index);
        if (index == 10087) {
            loadData();
        }
    }
}

